package com.supermap.dataservice.user;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.user.dao.UserDao;
import com.supermap.dataservice.user.dao.UserTypeDao;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dataservice.util.MD5Util;
import com.supermap.dataservice.util.Table;
import com.supermap.dubbo.api.common.entity.Area;
import com.supermap.dubbo.api.user.UserService;
import com.supermap.dubbo.api.user.entity.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:langhongshuai
 * @Description:用户业务逻辑层
 * @Date:18/1/3 下午4:23
 */
@Service(timeout = 1000000)
public class UserServiceImpl implements UserService {

    /**
     * 使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Resource
    private UserDao userDao;

    @Resource
    private UserTypeDao userTypeDao;

    //初始化MD5工具类
    private MD5Util md5Util = new MD5Util();

    // 因为必须要有返回值，才能保存到数据库中，如果保存的对象的某些字段是需要数据库生成的，
    //那保存对象进数据库的时候，就没必要放到缓存了
    @Override
    //必须要有返回值，否则没数据放到缓存中
    public User insertUser(User u) {
        this.userDao.insert(u);
        //u对象中可能只有只几个有效字段，其他字段值靠数据库生成，比如id
        return this.userDao.find(u.getLoginName());
    }

    @Override
    public JSONUtil findUserByAreaRId(String areaCode, int RoleId) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            List<User> users = userDao.findUserByAreaRId(areaCode, RoleId);

            //通过地区和角色后返回用户信息
            jsonUtil.setMsg("Get users success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(users);
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("RoleServiceImpl=>getAllRoles=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("Get users flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public BindUser queryBinUser(String userId) {
        BindUser bindUser = null;
        if (userId != null && !"".equals(userId)) {
            bindUser = userDao.queryBindUserName(userId);
        }
        return bindUser;
    }

    @Override
    public JSONUtil insertBindUser(String userId, String loginName, String password) {
        JSONUtil jsonUtil = new JSONUtil();
        if (loginName != null && !"".equals(loginName) && password != null && !"".equals(password)) {
            BindUser bindUser = userDao.queryBindUserName(userId);
            if (bindUser != null && !"".equals(bindUser)) {
                jsonUtil.setCode(450);
                jsonUtil.setIsSuccess(false);
                jsonUtil.setMsg("该用户已绑定！用户名：" + bindUser.getLoginName());
            } else {
                userDao.insertBindUser(userId, loginName, password);
                jsonUtil.setCode(200);
                jsonUtil.setIsSuccess(true);
                jsonUtil.setMsg("用户绑定成功！");
            }
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil updateStatus(int userId) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //分页获取用户列表
            User user = queryUser(userId);
            if (user.getDel_flg() == 0 || user.getDel_flg() == (Integer.parseInt(""))) {
                userDao.updateStatus(userId);
                //分页返回用户信息
                jsonUtil.setMsg("Get users success！");
                jsonUtil.setIsSuccess(true);
                jsonUtil.setCode(200);
                jsonUtil.setData(userId);
            } else {
                logger.info("该用户状态为无效");
            }
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("UserServiceImpl=>delUserById=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("Get users flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil resetPasswordCQ(int userId, String newPassword) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        //给密码加密
        String md5NewPassword = null;
        try {
            md5NewPassword = md5Util.EncoderByMd5(newPassword);
            //通过用户名和密码检查用户是否存在
        } catch (NoSuchAlgorithmException e) {
            logger.error("UserServiceImpl=>resetPassword=>NoSuchAlgorithmException:" + e.getMessage());
        } catch (UnsupportedEncodingException e) {
            logger.error("UserServiceImpl=>resetPassword=>UnsupportedEncodingException:" + e.getMessage());
        }

        try {
            //重置密码
            User user = userDao.queryUser(userId);
            if (user.getId() > 0) {
                userDao.resetPasswordCQ(userId, md5NewPassword);
                //重置密码成功后返回的json数据
                jsonUtil.setMsg("Reset password success！");
                jsonUtil.setIsSuccess(true);
                jsonUtil.setCode(200);
                return jsonUtil;
            } else {
                logger.info("该用户不存在");
            }


        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("UserServiceImpl=>resetPassword=>DaoException:" + e.getMessage());
            //重置密码失败后返回的json数据
            jsonUtil.setMsg("Reset password failed！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }

        return jsonUtil;
    }

    @Override
    public JSONUtil insertUserCQ(InterNetInfo us) {
        JSONUtil jsonUtil = new JSONUtil();

        if (us.getLoginName() != null && !"".equals(us.getMobile()) && us.getMobile() != null && !"".equals(us.getMobile())) {
            int num = queryMobile(us.getMobile());
            if (num > 0) {
                jsonUtil.setMsg("添加用户失败，电话号码已经存在！");
                jsonUtil.setIsSuccess(false);
                jsonUtil.setCode(400);
                logger.info("电话号码已经存在");
            } else {
                int id = queryLoginName(us.getLoginName());
                if (id > 0) {
                    jsonUtil.setMsg("添加用户失败，登陆名已经存在，请更换登陆名称！");
                    jsonUtil.setIsSuccess(false);
                    jsonUtil.setCode(400);
                    logger.info("登陆名已经存在");
                } else {
                    if (us.getArea() != null && !"".equals(us.getArea())) {
                        us.setAreaCode(us.getArea());
                    }
                    //获取区域信息
                    Area area = userDao.getArea(us.getAreaCode());
                    if (String.valueOf(area.getLatitude()) != null && String.valueOf(area.getLongitude()) != null) {
                        us.setLatitude(String.valueOf(area.getLatitude()));
                        us.setLongitude(String.valueOf(area.getLongitude()));
                    } else {
                        us.setLatitude(String.valueOf(""));
                        us.setLongitude(String.valueOf(""));
                    }

                    int numUs = this.userDao.insertCQ(us);
                    if (numUs > 0) {
                        User user = userDao.queryVipUser(us.getMobile());
                        TUserRole tUserRole = userTypeDao.getUserRoleId(user.getId());
                        if (tUserRole != null) {
                            userTypeDao.updateUserRole(String.valueOf(user.getId()), us.getRolecode());
                        } else {
                            userTypeDao.insertUserRole(String.valueOf(user.getId()), us.getRolecode());
                        }
                        us.setId(user.getId());
                        if (!"".equals(us.getRolecode()) && us.getRolecode() != null) {
                            if (us.getRolecode().equals("2")) {
                                int numRole = userTypeDao.insertZZDH(us);
                                resultJson(numRole > 0);
                            } else if (us.getRolecode().equals("3")) {
                                int numRole = userTypeDao.insertEXPERT(us);
                                resultJson(numRole > 0);
                            } else if (us.getRolecode().equals("4")) {
                                int numRole = userTypeDao.insertQYYH(us);
                                resultJson(numRole > 0);
                            } else if (us.getRolecode().equals("6")) {
                                int numRole = userTypeDao.insertHZSYH(us);
                                resultJson(numRole > 0);
                            } else if (us.getRolecode().equals("7")) {
                                int numRole = userTypeDao.insertGXS(us);
                                resultJson(numRole > 0);
                            } else if (us.getRolecode().equals("10")) {
                                int numRole = userTypeDao.insertZMXC(us);
                                resultJson(numRole > 0);
                            }
                        }
                    } else {
                        jsonUtil.setCode(200);
                        jsonUtil.setMsg("数据为空!");
                        jsonUtil.setIsSuccess(true);
                    }
                    jsonUtil.setMsg("添加用户成功！");
                    jsonUtil.setIsSuccess(true);
                    jsonUtil.setCode(200);
                }
            }
        } else {
            jsonUtil.setMsg("添加用户失败，电话或登录名不能为空！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(400);
            logger.info("电话或登录名已经存在");
        }
        return jsonUtil;
    }

    /**
     * 返回json
     **/
    public JSONUtil resultJson(Boolean b) {

        JSONUtil jsonUtil = new JSONUtil();
        if (b) {
            jsonUtil.setMsg("更新成功!");
            jsonUtil.setCode(200);
            jsonUtil.setIsSuccess(true);
        } else {
            jsonUtil.setMsg("更新失败!");
            jsonUtil.setCode(200);
            jsonUtil.setIsSuccess(true);
        }
        return jsonUtil;
    }

    @Override
    public int queryLoginName(String loginName) {
        return userDao.queryLoginName(loginName);
    }

    @Override
    public int queryMobile(String mobile) {
        return userDao.queryMobile(mobile);
    }

    @Override
    public List<Area> getAreaInfo() {
        return userDao.getAreaInfo();
    }

    @Override
    public List<Area> getProvince() {
        List<Area> province = userDao.getPro();
        List<Area> citys;
        List<Area> countys;
        if (province != null && !province.isEmpty()) {
            for (Area area : province) {
                citys = getCity(area.getAreaCode());
                area.setChildNode(citys);
                if (citys != null) {
                    for (Area area2 : citys) {
                        countys = getCounty(area2.getAreaCode());
                        area2.setChildNode(countys);
                    }
                }
            }
        }
        return province;
    }

    @Override
    public List<Area> getCity(String areaCode) {

        List<Area> city = userDao.getChinaArea(areaCode,"city");
        return city;
    }

    @Override
    public List<Area> getCounty(String areaCode) {
        List<Area> county = userDao.getChinaArea(areaCode,"county");
        return county;
    }

    @Override
    public List<Role> getRole() {
        return userDao.getRole();
    }

    @Override
    public User queryUser(int userId) {
        return userDao.queryUser(userId);
    }

    @Override
    public JSONUtil resetPassword(String userName, String oldPassword, String newPassword) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        //给密码加密
        String md5OldPassword = null;
        String md5NewPassword = null;
        try {
            md5OldPassword = md5Util.EncoderByMd5(oldPassword);
            md5NewPassword = md5Util.EncoderByMd5(newPassword);
            //通过用户名和密码检查用户是否存在
        } catch (NoSuchAlgorithmException e) {
            logger.error("UserServiceImpl=>resetPassword=>NoSuchAlgorithmException:" + e.getMessage());
        } catch (UnsupportedEncodingException e) {
            logger.error("UserServiceImpl=>resetPassword=>UnsupportedEncodingException:" + e.getMessage());
        }

        try {
            User user = userDao.checkUserByUP(userName, md5OldPassword);
            if (user != null) {
                //重置密码
                userDao.resetPassword(userName, md5NewPassword);
                //重置密码成功后返回的json数据
                jsonUtil.setMsg("Reset password success！");
                jsonUtil.setIsSuccess(true);
                jsonUtil.setCode(200);
            } else {
                //重置密码失败后返回的json数据
                jsonUtil.setMsg("Reset password failed！");
                jsonUtil.setIsSuccess(false);
                jsonUtil.setCode(100);
            }
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("UserServiceImpl=>resetPassword=>DaoException:" + e.getMessage());
            //重置密码失败后返回的json数据
            jsonUtil.setMsg("Reset password failed！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }

        return jsonUtil;
    }

    @Override
    public JSONUtil updateUserById(User user) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            int i = userDao.updateUserById(user);
            //更新用户角色,先判断是否存在
            List<Role> roles = userTypeDao.getRoleList();
            String roleCode = user.getRolecode();
            for (Role role : roles) {
                if (String.valueOf(role.getId()).equals(roleCode)) {
                    if (userTypeDao.existUserRole(String.valueOf(user.getId()), String.valueOf(role.getId())) > 0) {
                        userTypeDao.updateUserRole(String.valueOf(user.getId()), String.valueOf(role.getId()));
                    } else {
                        userTypeDao.deleteUserRole(String.valueOf(user.getId()));
                        userTypeDao.insertUserRole(String.valueOf(user.getId()), String.valueOf(role.getId()));
                    }
                }
            }

            User u = userDao.findUserById(String.valueOf(user.getId()));
            if (i == 1) {
                jsonUtil.setMsg("update user success！");
                jsonUtil.setIsSuccess(true);
                jsonUtil.setCode(200);
                jsonUtil.setData(u);
            } else {
                jsonUtil.setMsg("update user failed！");
                jsonUtil.setIsSuccess(false);
                jsonUtil.setCode(100);
            }
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("UserServiceImpl=>update=>DaoException:" + e.getMessage());
            //重置密码失败后返回的json数据
            jsonUtil.setMsg("update user failed！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getURLByAreaCode(String areaCode) {
        JSONUtil jsonUtil = new JSONUtil();
        String IP = "";
        switch (areaCode) {
            case "50":
                IP = "http://118.25.235.213:8020/";
                break;
            case "15":
                IP = "http://222.74.231.210:28030/";
                break;
            case "22":
                IP = "http://218.62.41.108:8020/";
                break;
            default:
                IP = "http://39.107.24.230:8030/";
                break;
        }
        jsonUtil.setCode(200);
        jsonUtil.setIsSuccess(true);
        jsonUtil.setData(IP);
        jsonUtil.setMsg("getURLByAreaCode success");
        return jsonUtil;
    }

    @Override
    public JSONUtil searchUserInfoByKeyWord(String keyWord, int page, int count, String areaCode) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            List<User> users = userDao.searchUserInfoByKeyWord(keyWord, page, count, areaCode);
            for (User user : users) {
                user.setPassword(null);
            }

            //通过地区和角色后返回用户信息
            jsonUtil.setMsg("Get users success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(users);
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("RoleServiceImpl=>getAllRoles=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("Get users flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil searchVip(String showName, String type, String areaCode, int page, int count) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();

        if (!showName.contains("%")) {
            showName = "%" + showName + "%";
        }

        if (!areaCode.contains("%")) {
            areaCode = areaCode + "%";
        }

        try {
            List<Vip> users = userDao.searchVip(showName, type, areaCode, page, count);

            //通过地区和角色后返回用户信息
            jsonUtil.setMsg("Get users success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(users);
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("RoleServiceImpl=>getAllRoles=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("Get users flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil updateUser(User user) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            int i = userDao.updateUser(user);
            User u = userDao.findUserById(String.valueOf(user.getId()));
            if (i == 1) {
                jsonUtil.setMsg("update user success！");
                jsonUtil.setIsSuccess(true);
                jsonUtil.setCode(200);
                jsonUtil.setData(u);
            } else {
                jsonUtil.setMsg("update user failed！");
                jsonUtil.setIsSuccess(false);
                jsonUtil.setCode(100);
            }
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("UserServiceImpl=>update=>DaoException:" + e.getMessage());
            //重置密码失败后返回的json数据
            jsonUtil.setMsg("update user failed！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getUserList(Table table, User user) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //分页获取用户列表
            List<User> users = userDao.getUserList(table, user);
            int count = userDao.getUserListCount(table, user);
            table.setCount(count);
            table.setData(users);
            //分页返回用户信息
            jsonUtil.setMsg("Get users success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(table);
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("RoleServiceImpl=>getUserList=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("Get users flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getAppUserList(Table table, User user) {
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //分页获取用户列表
            List<User> users = userDao.getAppUserList(table, user);
            int count = userDao.getUserListCount(table, user);
            table.setCount(count);
            table.setData(users);
            //分页返回用户信息
            jsonUtil.setMsg("Get users success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(table);
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("RoleServiceImpl=>getUserList=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("Get users flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil delUserById(int userId) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //分页获取用户列表
            userDao.delUserById(userId);
            //分页返回用户信息
            jsonUtil.setMsg("Get users success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(userId);
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("RoleServiceImpl=>delUserById=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("Get users flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    private User findUserByUserId(int userId) {
        try {

            return userDao.findUserById(String.valueOf(userId));

        } catch (Exception e) {
        }
        return null;
    }

    @Override
    public JSONUtil applyForVIP(Vip param) {

        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {

            User user = this.findUserByUserId(param.getUserId());
            Vip vip = new Vip();
            vip.assistUserParam(user);
            vip.assistInformation(param);

            int i = userDao.applyForVIP(vip);
            Vip vip1 = userDao.findApplyVipById(String.valueOf(vip.getId()));
            if (i == 1) {
                jsonUtil.setMsg("applyForVIP user success！");
                jsonUtil.setIsSuccess(true);
                jsonUtil.setCode(200);
                jsonUtil.setData(vip1);
            } else {
                jsonUtil.setMsg("applyForVIP user failed！");
                jsonUtil.setIsSuccess(false);
                jsonUtil.setCode(100);
            }

        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("UserServiceImpl=>applyForVIP=>DaoException:" + e.getMessage());
            //重置密码失败后返回的json数据
            jsonUtil.setMsg("update user failed！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }

        return jsonUtil;
    }

    @Override
    public JSONUtil affirmToVIP(String userId) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {

            Vip vip = this.userDao.findApplyVipById(userId);
            int i = userDao.affirmToVIP(vip);
            i = userDao.applyVIPFlag(userId) * i;
            vip = this.userDao.findVipById(userId);
            if (i == 1) {
                jsonUtil.setMsg("applyForVIP user success！");
                jsonUtil.setIsSuccess(true);
                jsonUtil.setCode(200);
                jsonUtil.setData(vip);
            } else {
                jsonUtil.setMsg("applyForVIP user failed！");
                jsonUtil.setIsSuccess(false);
                jsonUtil.setCode(100);
            }

        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("UserServiceImpl=>applyForVIP=>DaoException:" + e.getMessage());
            //重置密码失败后返回的json数据
            jsonUtil.setMsg("update user failed！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }

        return jsonUtil;
    }

    @Override
    public List<User> usersExcel(Table table, User user) {

        return userDao.getUserList(table, user);
    }

    @Override
    public JSONUtil queryVipUserList(Table table, User user, Integer vipId) {
        JSONUtil jsonUtil = new JSONUtil();
        Vip vip = userDao.getVip(vipId);
        if (vip != null) {

            int count = userDao.getVipUserListCount(table, user, vip.getId());
            List<User> users = userDao.getVipUserList(table, user, vip.getId());
            table.setCount(count);
            table.setData(users);
            //分页返回用户信息
            jsonUtil.setMsg("Get users success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(table);
        } else {
            jsonUtil.setMsg("Get users fail！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(400);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil updateUserlatlongByUserId(String UserId, BigDecimal longitude, BigDecimal latitude) {
        //查询出用户ID
        Map<String, Object> map = userDao.getUserrlatlongByuserId(UserId);
        String areaCode = (String) map.get("areaCode");
        BigDecimal longi = (BigDecimal) map.get("longitude");
        BigDecimal lat = (BigDecimal) map.get("latitude");
        if (longi != null && lat != null) {
            Integer count = userDao.getAreaByList(areaCode, longi, lat);
            if (count > 0) {
                userDao.updateUserlatlongByUserId(UserId, longitude, latitude);
            }
        }

        return new JSONUtil();
    }

    @Override
    public JSONUtil updateState(String state, int id) {
        JSONUtil jsonUtil = new JSONUtil();
        if (id != 0) {
            int num = userDao.updateState(state, id);
            if (num > 0) {
                jsonUtil.setMsg("更新成功！");
                jsonUtil.setIsSuccess(true);
                jsonUtil.setCode(200);
            } else {
                jsonUtil.setMsg("更新失败！");
                jsonUtil.setIsSuccess(true);
                jsonUtil.setCode(200);
            }
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil addVipUser(User us, Integer vipId) {
        JSONUtil jsonUtil = new JSONUtil();
        Vip vip = userDao.getVip(vipId);
        if (us.getLoginName() != null && !"".equals(us.getMobile()) && us.getMobile() != null && !"".equals(us.getMobile())) {
            int num = userDao.queryMobile(us.getMobile());
            if (num > 0) {
                jsonUtil.setMsg("添加用户失败，电话号码已经存在！");
                jsonUtil.setIsSuccess(false);
                jsonUtil.setCode(400);
            } else {
                int id = userDao.queryLoginName(us.getLoginName());
                if (id > 0) {
                    jsonUtil.setMsg("添加用户失败，登陆名已经存在，请更换登陆名称！");
                    jsonUtil.setIsSuccess(true);
                    jsonUtil.setCode(400);
                } else {
                    int userNum = userDao.insertAPPUser(us);
                    if (userNum > 0) {
                        User user = userDao.queryVipUser(us.getMobile());
                        userDao.insertVipUserRelation(vip.getId(), user.getId());
                        jsonUtil.setMsg("添加用户成功！");
                        jsonUtil.setIsSuccess(true);
                        jsonUtil.setCode(200);
                    } else {
                        jsonUtil.setMsg("企业和员工关联失败！");
                        jsonUtil.setIsSuccess(true);
                        jsonUtil.setCode(301);
                    }
                }
            }
        } else {
            jsonUtil.setMsg("添加用户失败，电话或登录名不能为空！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(402);

        }
        return jsonUtil;
    }
}
